package com.tuozixuan.javafx.shape;


import javafx.application.Application;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class RectangleDemo extends Application {
	
    public static void main(String[] args) {
        launch(args);
    }

	@Override
	public void start(Stage primaryStage) throws Exception {
		
		Rectangle rectangle = new Rectangle();
		rectangle.setX(100);
		rectangle.setY(100);
		rectangle.setWidth(100);
		rectangle.setHeight(50);
		
		// 设置四个角的圆弧宽度和圆弧高度，用来绘制圆角矩形
		rectangle.setArcWidth(15);
		rectangle.setArcHeight(15);
		
		// 设置当鼠标悬停在矩形上时鼠标指针的显示
		rectangle.cursorProperty().set(Cursor.HAND);
		// 设置旋转角度
//		rectangle.setRotate(30);
		
		rectangle.setFill(Color.BLUE);
		
		rectangle.setOnMouseClicked(event -> {
			System.out.println("Rectangle be clicked..." + event);
		});
		// 定义在该节点上按下鼠标按钮然后拖动时要调用的函数
		rectangle.setOnMouseDragged(event -> {
			System.out.println("Rectangle be clicked..." + event);
			double x = event.getX() - rectangle.getWidth() / 2;
			double y = event.getY() - rectangle.getHeight() / 2;
			rectangle.setX(x);
			rectangle.setY(y);
		});
		rectangle.setOnKeyPressed(event -> {
			System.out.println("Rectangle be pressed..." + event.getCode().getName());
		});
		rectangle.setOnKeyReleased(event -> {
			System.out.println("Rectangle be released..." + event.getCode().getName());
		});
		rectangle.setOnContextMenuRequested(event -> {
			System.out.println("Rectangle be contextMenuRequested..." + event.getTarget());
		});
		rectangle.setOnDragDetected(event -> {
			System.out.println("Rectangle be dragDetected..." + event);
		});
		rectangle.setOnDragDone(event -> {
			System.out.println("Rectangle be dragDone..." + event);
		});
		rectangle.setOnDragDropped(event -> {
			System.out.println("Rectangle be dragDropped..." + event);
		});
		
		// 矩形移动到鼠标的位置
        Scene scene = new Scene(new Group(rectangle), 300, 300);
        scene.setOnMouseClicked(event -> {
			System.out.println("Rectangle be clicked..." + event);
			double x = event.getX() - rectangle.getWidth() / 2;
			double y = event.getY() - rectangle.getHeight() / 2;
			rectangle.setX(x);
			rectangle.setY(y);
		});
        
        // 矩形跟随鼠标移动
//        scene.setOnMouseMoved(event -> {
//			System.out.println("Rectangle be clicked..." + event);
//			double x = event.getX() - rectangle.getWidth() / 2;
//			double y = event.getY() - rectangle.getHeight() / 2;
//			rectangle.setX(x);
//			rectangle.setY(y);
//		});

        // 设置左上角图标
        primaryStage.getIcons().add(new Image("see.png"));
        primaryStage.getIcons().addAll(new Image("see.png"), new Image("see.png"));
        
        // 设置标题
        primaryStage.setTitle("Welcome to JavaFX!");
        
        primaryStage.setScene(scene);
        primaryStage.sizeToScene();
        primaryStage.show();
        
        // 隐藏窗体
        primaryStage.hide();
        primaryStage.show();
        
        // 关闭窗口，相当于点击窗口关闭图标
        //primaryStage.close();
	}

}
